package com.g1.gzbooksingle.controller;


import com.g1.gzbooksingle.model.DgBookInfo;
import com.g1.gzbooksingle.model.DgWeekRecommand;
import com.g1.gzbooksingle.service.IDgBookInfoService;
import com.g1.gzbooksingle.service.IDgWeekRecommandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 每周推荐表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-10-12
 */
@Api(tags = "每周推荐接口")
@RestController
@RequestMapping("/gzbooksingle/dgWeekRecommand")
public class DgWeekRecommandController {

    @Autowired
    private IDgWeekRecommandService dgWeekRecommandService;

    @Autowired
    private IDgBookInfoService dgBookInfoService;

    @GetMapping
    @ApiOperation("获取最新推荐")
    public List<DgBookInfo> weekRecommand(){
        DgWeekRecommand weekRecommand = dgWeekRecommandService.getNewWeekRecommand();
        // weekBooks = [6,7,8,9,10]
        String weekBooks = weekRecommand.getBooks();
        // weekBooks = 6,7,8,9,10
        weekBooks = weekBooks.replaceAll("([\\]\\[])","");
        String[] books = weekBooks.split(",");
        List<DgBookInfo> dgBookInfos = new ArrayList<>();
        for (String book : books) {
            dgBookInfos.add(dgBookInfoService.getById(book));
        }
        return dgBookInfos;
    }
}
